package com.cmc6.bulletin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc6.bulletin.model.dto.BulletinInsertOrUpdateDTO;
import com.cmc6.bulletin.model.dto.BulletinPageDTO;
import com.cmc6.bulletin.model.dto.BulletinUserPageDTO;
import com.cmc6.bulletin.model.entity.BulletinDO;
import com.cmc6.bulletin.model.vo.BulletinPageVO;
import com.cmc6.bulletin.model.vo.BulletinUserPageVO;
import com.cmc6.bulletin.service.BulletinService;
import com.cmc6.common.model.dto.NotEmptyIdSet;
import com.cmc6.common.model.dto.NotNullId;
import com.cmc6.common.util.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/bulletin")
@Api(tags = "公告控制器")
public class BulletinController {

    @Resource
    BulletinService baseService;

    @ApiOperation(value = "新增/修改 公告")
    @PostMapping("/insertOrUpdate")
    @PreAuthorize("hasAuthority('bulletin:insertOrUpdate')")
    public ApiResult<String> insertOrUpdate(@RequestBody @Valid BulletinInsertOrUpdateDTO dto) {
        return ApiResult.ok(baseService.insertOrUpdate(dto));
    }

    @ApiOperation(value = "发布 公告")
    @PostMapping("/submit")
    @PreAuthorize("hasAuthority('bulletin:insertOrUpdate')")
    public ApiResult<String> submit(@RequestBody @Valid NotNullId notNullId) {
        return ApiResult.ok(baseService.submit(notNullId));
    }

    @ApiOperation(value = "撤回 公告")
    @PostMapping("/revoke")
    @PreAuthorize("hasAuthority('bulletin:insertOrUpdate')")
    public ApiResult<String> revoke(@RequestBody @Valid NotNullId notNullId) {
        return ApiResult.ok(baseService.revoke(notNullId));
    }

    @ApiOperation(value = "分页排序查询：公告")
    @PostMapping("/page")
    @PreAuthorize("hasAuthority('bulletin:page')")
    public ApiResult<Page<BulletinPageVO>> myPage(@RequestBody @Valid BulletinPageDTO dto) {
        return ApiResult.ok(baseService.myPage(dto));
    }

    @ApiOperation(value = "通过主键id，查看详情")
    @PostMapping("/infoById")
    @PreAuthorize("hasAuthority('bulletin:infoById')")
    public ApiResult<BulletinDO> infoById(@RequestBody @Valid NotNullId notNullId) {
        return ApiResult.ok(baseService.infoById(notNullId));
    }

    @ApiOperation(value = "删除公告")
    @PostMapping("/deleteByIdSet")
    @PreAuthorize("hasAuthority('bulletin:deleteByIdSet')")
    public ApiResult<String> deleteByIdSet(@RequestBody @Valid NotEmptyIdSet notEmptyIdSet) {
        return ApiResult.ok(baseService.deleteByIdSet(notEmptyIdSet));
    }

    @PostMapping("/userPage")
    @ApiOperation(value = "分页排序查询：当前用户可以查看的公告")
    public ApiResult<Page<BulletinUserPageVO>> userPage(@RequestBody @Valid BulletinUserPageDTO dto) {
        return ApiResult.ok(baseService.userPage(dto));
    }

    @PostMapping("/userCount")
    @ApiOperation(value = "获取：当前用户可以查看的公告，总数")
    public ApiResult<Long> userCount() {
        return ApiResult.ok(baseService.userCount());
    }

}
